package pl.vojna.ant_alg;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Problem {

    public int[][] distances;
    public int[][] flows;

    public Problem(File file) {
        String content;
        try {
            content = new Scanner(file).useDelimiter("\\Z").next();
            parseText(content);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    /**
     * Tworzy nowy problem na podstawie tekstowej reprezentacji uzywanej w
     * QAPlib
     * 
     * @param str
     */
    public Problem(String str) {
        parseText(str);
    }

    private void parseText(String str) {
        StringTokenizer st = new StringTokenizer(str, " \n", false);
        int problemSize = Integer.valueOf(st.nextToken());
        distances = new int[problemSize][problemSize];
        flows = new int[problemSize][problemSize];
        for (int i = 0; i < problemSize; i++) {
            for (int j = 0; j < problemSize; j++) {
                flows[i][j] = Integer.valueOf(st.nextToken());
            }
        }
        for (int i = 0; i < problemSize; i++) {
            for (int j = 0; j < problemSize; j++) {
                distances[i][j] = Integer.valueOf(st.nextToken());
            }
        }
    }

    public Problem(int[][] distances, int[][] flows) {
        this.distances = distances;
        this.flows = flows;
    }

    public int getSize() {
        return distances.length;
    }

    @Override
    public String toString() {
        StringBuilder st = new StringBuilder();
        st.append("FLOWS: \n");
        for (int i = 0; i < flows.length; i++) {
            for (int j = 0; j < flows.length; j++) {
                st.append(flows[i][j]);
                st.append(" ");
            }
            st.append("\n");
        }
        st.append("DISTANCES: \n");
        for (int i = 0; i < distances.length; i++) {
            for (int j = 0; j < distances.length; j++) {
                st.append(distances[i][j]);
                st.append(" ");
            }
            st.append("\n");
        }
        return st.toString();
    }

    public void wirteToFile(String filename) {
        FileWriter fw = null;
        PrintWriter pw = null;
        File f = null;
        try {
            f = new File(filename);
            fw = new FileWriter(f);
            pw = new PrintWriter(fw);
            int size = flows.length;
            pw.println(size);
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    pw.print(distances[i][j]+" ");
                }
                pw.println();
            }
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    pw.print(flows[i][j]+" ");
                }
                pw.println();
            }


        } catch (IOException ex) {
            System.out.println(ex.getMessage());

        } finally {
            try {
                pw.close();
                fw.close();
            } catch (IOException ex) {
                Logger.getLogger(Problem.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    static Problem readFile(String fileName) {
        FileReader fr = null;
        BufferedReader br = null;
        try {
            File f = new File(fileName);
            fr = new FileReader(f);
            br = new BufferedReader(fr);
            int size = Integer.valueOf(br.readLine());
            System.out.println(size);
            int flows[][] = new int[size][size];
            int distances[][] = new int[size][size];
            String line;
            String trimmedLine[];
            for (int i = 0; i < size; i++) {
                line = br.readLine();
                trimmedLine = line.split(" ");
                for (int j = 0; j < size; j++) {
                    distances[i][j] = Integer.valueOf(trimmedLine[j]);
                }

            }
            for (int i = 0; i < size; i++) {

                line = br.readLine();
                trimmedLine = line.split(" ");
                
                for (int j = 0; j < size; j++) {
                    flows[i][j] = Integer.valueOf(trimmedLine[j]);
                }


            }
            Problem result = new Problem(distances, flows);
            return result;
        } catch (IOException ex) {
            System.out.println(ex.getMessage());


            return null;
        } finally {
            try {
                fr.close();
                br.close();
            } catch (IOException ex) {
                Logger.getLogger(Problem.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
